class gravityField 
{
  public vec2d[][] field;
  public planet[] planets;

  private float G;
  private float gravVecMult;

  private int gridH, gridW;
  private int dataRes, gridRes;
  public int dataH, dataW;

  private int numOfPlanets;
  
  public boolean displayVectors;

  gravityField(int gridH, int gridW, int dataRes, int gridRes, float G)
  {
    this.dataRes = dataRes;
    this.gridRes = gridRes;
    
    this.G = G;
    
    this.gridW = gridW * gridRes;
    this.gridH = gridH * gridRes;

    this.dataH = this.gridH / this.dataRes;
    this.dataW = this.gridW / this.dataRes;

    this.numOfPlanets = 0;
    this.planets = new planet[numOfPlanets];

    this.field = new vec2d[this.dataW][this.dataH];

    

    this.gravVecMult = 300;
    
    this.displayVectors = false;
  }


  public void generatePlanetsRandom(int numOfPlanets, float minMass, float maxMass)
  {
    this.numOfPlanets = numOfPlanets;
    this.planets = new planet[this.numOfPlanets];


    for(int i = 0; i < this.numOfPlanets; i++)
    {
      float mass = random(minMass, maxMass);
      
      int rad = (int)map(mass, minMass, maxMass, 75, 125);
      
      this.planets[i] = new planet(random(0,this.gridW),random(0,this.gridH), mass, rad, color(255, 255, 255)); 
    }
  }

  public void generateGravityField()
  {
    for(int x = 0; x < this.dataW; x++)
    {
      for(int y = 0; y < this.dataH; y++)
      {
        this.field[x][y] = new vec2d();
        this.field[x][y].x = 0;
        this.field[x][y].y = 0;

        for(int p = 0; p < this.numOfPlanets; p++)
        {
          float m = 1; // For the field we'll use a mass of 1 unit so it's easy to work with later on.
          float M = this.planets[p].mass;

          float dx = this.planets[p].position.x - (x * this.dataRes);
          float dy = this.planets[p].position.y - (y * this.dataRes);
          float d = (float)Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));

          if(d < this.planets[p].rad)
          {
            d = this.planets[p].rad;
          }

          float g = (G * m * M) / (float)Math.pow(d, 2);

          this.field[x][y].x += g * (dx/d);
          this.field[x][y].y += g * (dy/d);
        }
      }
    }
  }
}
